package com.hmall.order.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.client.ItemClient;
import com.hmall.client.UserClient;
import com.hmall.common.UserHolder;
import com.hmall.item.Item;
import com.hmall.order.Order;
import com.hmall.order.OrderDetail;
import com.hmall.order.OrderDto;
import com.hmall.order.OrderLogistics;
import com.hmall.order.mapper.OrderDetailMapper;
import com.hmall.order.mapper.OrderLogisticsMapper;
import com.hmall.order.mapper.OrderMapper;

import com.hmall.order.service.IOrderService;
import com.hmall.user.Address;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

//完成订单提交功能，完成mq和feign调用，未测试，没写seata事务

@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired(required = false)
    private ItemClient itemClient;

    @Autowired(required = false)
    private UserClient userClient;

    @Autowired(required = false)
    private OrderMapper orderMapper;

    @Autowired(required = false)
    private OrderDetailMapper orderDetailMapper;

    @Autowired(required = false)
    private OrderLogisticsMapper orderLogisticsMapper;

    @Autowired(required = false)
    private RabbitTemplate rabbitTemplate;

    /**
     * @param orderDto OrderDto
     * @return Long
     * 提交订单
     */
    @GlobalTransactional //全局事务
    @Override
    public Long submit(OrderDto orderDto) {

        //通过feign远程调用，根据id查询商品
        Item dbitem = itemClient.findById(orderDto.getItemId());

        if (orderDto.getNum()<=dbitem.getStock()) {
            //通过feign远程调用，根据id查询地址
            Address dbaddress = userClient.selectAddressById(orderDto.getAddressId());

            //订单总价
            Long totalFee = dbitem.getPrice() * orderDto.getNum();

            //补全订单数据
            Order order = new Order();
            Date date = new Date();
            //补全付款类型
            BeanUtils.copyProperties(orderDto, order);
            order.setTotalFee(totalFee);
            order.setUserId(UserHolder.getUser());//登入用户id
            order.setStatus(1);
            order.setCreateTime(date);
            order.setUpdateTime(date);

            //插入订单表
            orderMapper.insert(order);

            //补全订单详情数据
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setNum(orderDto.getNum());
            BeanUtils.copyProperties(dbitem, orderDetail);
            BeanUtils.copyProperties(order, orderDetail);
            orderDetail.setOrderId(order.getId());
            orderDetail.setItemId(dbitem.getId());


            //插入订单详情表
            orderDetailMapper.insert(orderDetail);

            //补全物流详情数据
            OrderLogistics orderLogistics = new OrderLogistics();
            BeanUtils.copyProperties(dbaddress, orderLogistics);
            orderLogistics.setOrderId(order.getId());
            orderLogistics.setCreateTime(order.getCreateTime());
            orderLogistics.setUpdateTime(order.getUpdateTime());

            //插入物流表
            orderLogisticsMapper.insert(orderLogistics);

            //order拷贝到orderDto
            BeanUtils.copyProperties(order, orderDto);


            //通过MQ远程调用，根据购买数量扣减库存
            //itemClient.updateByNum(orderDto.getNum());
            rabbitTemplate.convertAndSend("item.order", "sub", orderDto);

            //清理超时未支付订单

            //mq发送orderDto到order交换机
            rabbitTemplate.convertAndSend("order.exchange", "order.key", orderDto);

            //获取订单id
            Long orderId = order.getId();
            return orderId;
        }else {
            throw new RuntimeException("库存不足");
        }


    }

    /**
     * @param OrderId
     * @return void
     * 根据订单id-修改订单状态-修改交易关闭时间
     */
    public void updateOrderStatusAndCloseTime(Long OrderId, Date date) {

        orderMapper.updateOrderById(OrderId, date);

    }
}
